import * as vscode from "vscode";
import { addComment } from "../plugins/actions/AddComment/addComment";
import { optimizeCode } from "../plugins/actions/OptimizeCode/optimizeCode";
import { VerticalDiffManager } from "./diff/vertical/manager";
import { LLMFactory } from "../core/llm/llmFactory";
import { HarmonyPilotGUIWebviewViewProvider } from "./HarmonyPilotGUIWebviewViewProvider";
import { focusInput } from "../plugins/actions/FocusInput/focusInput";
import { CommentCollectionFactory, CommentCollectionType } from "../core/comments/CommentCollectionFactory";
import { aiReview } from "../plugins/actions/AiReview/aiReview";
import { HarmonyPilotCommentCollectionProvider } from "../core/comments/HarmonyPilotCommentCollectionProvider";
// Copy everything over from extension.ts
const getCommandsMap: (verticalDiffManager: VerticalDiffManager, llmFactory: LLMFactory, 
    sidebar: HarmonyPilotGUIWebviewViewProvider, commentCollectionFactory: CommentCollectionFactory) 
=> { [command: string]: (...args: any) => any } = 
(verticalDiffManager: VerticalDiffManager, llmFactory: LLMFactory, sidebar: HarmonyPilotGUIWebviewViewProvider, commentCollectionFactory: CommentCollectionFactory) => {
    return {
        "HarmonyPilot.focusInput": async () => {
            focusInput(sidebar);
        },

        "HarmonyPilot.newChatBtnClick": async () => {
            //TODO: 实现New Chat命令
        },

        "HarmonyPilot.chatHistoryBtnClick": async () => {
            //TODO: 实现Chat History命令
        },

        "HarmonyPilot.helpBtnClick": async () => {
            //TODO: 实现Help命令
        }, 

        "HarmonyPilot.issueReportBtnClick": async () => {
            //TODO: 实现Issue Report命令
        },

        "HarmonyPilot.settingBtnClick": async () => {   
            //TODO: 实现Setting命令
        },
        "HarmonyPilot.rejectVerticalDiffBlock": async(fileUri?: string, index?: number) => {
            verticalDiffManager.acceptRejectVerticalDiffBlock(false, fileUri, index);
        }, 

        "HarmonyPilot.acceptVerticalDiffBlock": async(fileUri?: string, index?: number) => {
            verticalDiffManager.acceptRejectVerticalDiffBlock(true, fileUri, index);
        },
    
        // Write Comment for Code
        "HarmonyPilot.writeCommentForCode": async () => {   
            addComment(verticalDiffManager, llmFactory);
        },
        
        // Optimize Code
        "HarmonyPilot.optimizeCode": async () => {
            optimizeCode(verticalDiffManager, llmFactory);
        },

        // Apply And Go To Next
        'harmonypilot.applyAndGoToNext': (threadUuid: string) => {
                const collection = HarmonyPilotCommentCollectionProvider.getCollection(threadUuid);
                if (collection == undefined) {
                    return;
                }
                collection.applyAndGoToNext(threadUuid);
        },

        // Discard And Go To Next
        'harmonypilot.discardAndGoToNext': (threadUuid: string) => {
            const collection = HarmonyPilotCommentCollectionProvider.getCollection(threadUuid);
            if (collection == undefined) {
                return;
            }
            collection.discardAndGoToNext(threadUuid);
        },

        // Go To Next
        'harmonypilot.goToNextByTitle': (thread: vscode.CommentThread) => {
            const uuid = HarmonyPilotCommentCollectionProvider.getUuidByThread(thread);
            if (uuid == undefined) {
                return;
            }
            const collection = HarmonyPilotCommentCollectionProvider.getCollection(uuid);
            if (collection == undefined) {
                return;
            }
            collection.goToNextComment(uuid);
        },

        // Go To Previous
        'harmonypilot.goToPreviousByTitle': (thread: vscode.CommentThread) => {
            const uuid = HarmonyPilotCommentCollectionProvider.getUuidByThread(thread);
            if (uuid == undefined) {
                return;
            }
            const collection = HarmonyPilotCommentCollectionProvider.getCollection(uuid);
            if (collection == undefined) {
                return;
            }
            collection.goToPreviousComment(uuid);
        },

        // AI Review
        "HarmonyPilot.aiReview": async () => {
            const commentCollection = commentCollectionFactory.getCommentCollection(CommentCollectionType.AI_REVIEW);
            if (!commentCollection) {
                vscode.window.showErrorMessage("AI Review comment collection not found");
                return;
            }
            // 必须要做
            commentCollection.clear();
            aiReview(commentCollection, llmFactory);
        }
    };
};

export function registerAllCommands(
    context: vscode.ExtensionContext,
    verticalDiffManager: VerticalDiffManager,
    llmFactory: LLMFactory,
    sidebar: HarmonyPilotGUIWebviewViewProvider,
    commentCollectionFactory: CommentCollectionFactory
) {
    for (const [command, callback] of Object.entries(
        getCommandsMap(verticalDiffManager, llmFactory, sidebar, commentCollectionFactory)
    )) {
        // 注册所有命令
        context.subscriptions.push(
            vscode.commands.registerCommand(command, callback),
          );
    }
}
